package com.itzxx.huazhun.utils.ble

import android.util.Log
import com.google.gson.Gson
import com.itzxx.library_base_ktx.ext.util.loge
import org.json.JSONObject
import java.util.UUID

@OptIn(ExperimentalUnsignedTypes::class)
object BleParseData {
    /**
     * 处理底座回复数据
     */
    @OptIn(ExperimentalUnsignedTypes::class)
    fun handleCoordinatorResponse(data: UByteArray): String {
        if (dataVerification(data)) {
            when (getCmd(data)) {
                BleConstant.Cmd.GET_COORDINATOR_INFO_RESPONSE -> {
                    return handleCoordinatorInfoResponse(data)
                }

                BleConstant.Cmd.GET_PROBE_INFO_RESPONSE -> {
                    return handleProbeInfoResponse(data)
                }

                BleConstant.Cmd.READ_SAMPLING_INFO_RESPONSE -> {
                    return handleReadSamplingInfoResponse(data)
                }

                BleConstant.Cmd.READ_SAMPLING_DATA_RESPONSE -> {
                    return handleReadSamplingDataResponse(data)
                }

                BleConstant.Cmd.SET_PROBE_PARAMETER_RESPONSE -> {
                    return handleSetProbeParameterResponse(data)
                }

                BleConstant.Cmd.CLEAR_PROBE_SAMPLING_DATA_RESPONSE -> {
                    return handleClearProbeSamplingDataResponse(data)
                }

                BleConstant.Cmd.AIR_PRESSURE_CALIBRATION_RESPONSE -> {
                    return handleAirPressureCalibrationResponse(data)
                }
            }
        }
        return JSONObject().toString()
    }

    /**
     * 处理底座回复数据
     */
    @OptIn(ExperimentalUnsignedTypes::class)
    fun handleCmdTypeDResponse(data: UByteArray): UByte {
        if (dataVerification(data)) {
           return getCmd(data)
        }
        return 0x00u
    }

    @OptIn(ExperimentalUnsignedTypes::class)
    private fun handleAirPressureCalibrationResponse(data: UByteArray): String {
        return handleCommonResponse(data, "handleAirPressureCalibrationResponse")
    }

    @OptIn(ExperimentalUnsignedTypes::class)
    private fun handleClearProbeSamplingDataResponse(data: UByteArray): String {
        return handleCommonResponse(data, "handleClearProbeSamplingDataResponse")
    }

    @OptIn(ExperimentalUnsignedTypes::class)
    private fun handleSetProbeParameterResponse(data: UByteArray): String {
        return handleCommonResponse(data, "handleClearProbeSamplingDataResponse")
    }

    @OptIn(ExperimentalUnsignedTypes::class)
    private fun handleReadSamplingDataResponse(data: UByteArray): String {
        val mac = getMac(data, 4, BleConstant.MAC_BYTE)
        val readIndex = byteArray2Long(data.copyOfRange(12, 16).toByteArray())
        val readNum = byteArray2Int(data.copyOfRange(16, 20).toByteArray())
        val recordData = getRecordData(data, 20,  readNum)
//        val info = """
//                mac : $mac
//                readIndex : $readIndex
//                readNum : $readNum
//                recordData : $recordData
//            """.trimIndent()
//        Log.e("handleReadSamplingDataResponse", info)
        val jsonObject = JSONObject()
        jsonObject.put("mac", mac)
        jsonObject.put("readIndex", readIndex)
        jsonObject.put("readSize", readNum)
        jsonObject.put("recordData", Gson().toJson(recordData))
        return jsonObject.toString()
    }

    @OptIn(ExperimentalUnsignedTypes::class)
    private fun handleReadSamplingInfoResponse(data: UByteArray): String {
        val mac = getMac(data, 4, BleConstant.MAC_BYTE)
        val readIndex = data[12].toInt()
        val readSize = data[13].toInt()
        val samplingInfo = getSamplingInfo(data, 14, 16, readSize)
//        val info = """
//                mac : $mac
//                readIndex : $readIndex
//                readSize : $readSize
//                samplingInfo : $samplingInfo
//            """.trimIndent()
//        Log.e("handleReadSamplingInfoResponse", info)
        val jsonObject = JSONObject()
        jsonObject.put("mac", mac)
        jsonObject.put("readIndex", readIndex)
        jsonObject.put("readSize", readSize)
        jsonObject.put("samplingInfo", Gson().toJson(samplingInfo))
        UUID.randomUUID()
        return jsonObject.toString()
    }

    @OptIn(ExperimentalUnsignedTypes::class)
    private fun handleCoordinatorInfoResponse(data: UByteArray): String {
        val coordinatorMac = getMac(data, 4, BleConstant.MAC_BYTE)
        val sw = data[12].toInt()
        val hw = data[13].toInt()
        val model = getModel(data, 14, 10)
        val holeNum = data[24].toInt()
        val probeNum = data[25].toInt()
        val probe = getProbes(data, 26, BleConstant.MAC_BYTE, probeNum)
        val info = """
                coordinatorMac : $coordinatorMac
                sw : $sw
                hw : $hw
                model : $model
                holeNum :$holeNum
                probeNum :$probeNum
                probe : $probe
            """.trimIndent()
        Log.e("handleCoordinatorInfoResponse", info)
        val jsonObject = JSONObject()
        jsonObject.put("coordinatorMac", coordinatorMac)
        jsonObject.put("sw", sw)
        jsonObject.put("hw", hw)
        jsonObject.put("model", model)
        jsonObject.put("holeNum", holeNum)
        jsonObject.put("probeNum", probeNum)
        jsonObject.put("probe", probe)
        return jsonObject.toString()
    }

    @OptIn(ExperimentalUnsignedTypes::class)
    fun handleProbeInfoResponse(data: UByteArray): String {
        val mac = getMac(data, 4, BleConstant.MAC_BYTE)
        val sw = data[12].toInt()
        val hw = data[13].toInt()
        val model = getModel(data, 14, 10)
        val battery = data[24].toInt()
        val feature = data[25].toInt()
        val wayNum = data[26].toInt()
        val wayIndex = getWayIndex(data, 27, wayNum)
        val samplingTimesCapacity = data[27 + wayNum]
        val currentSamplingTimes = data[27 + wayNum + 1]
//        val info = """
//                mac :$mac
//                sw :$sw
//                hw :$hw
//                model : $model
//                battery: $battery
//                feature :$feature
//                wayNum :$wayNum
//                wayIndex :$wayIndex
//                samplingTimesCapacity :$samplingTimesCapacity
//                currentSamplingTimes :$currentSamplingTimes
//            """.trimIndent()
//        Log.e("handleProbeInfoResponse", info)
        val jsonObject = JSONObject()
        jsonObject.put("mac", mac)
        jsonObject.put("sw", sw)
        jsonObject.put("hw", hw)
        jsonObject.put("model", model)
        jsonObject.put("battery", battery)
        jsonObject.put("feature", feature)
        jsonObject.put("wayNum", wayNum)
        jsonObject.put("wayIndex", wayIndex)
        jsonObject.put("samplingTimesCapacity", samplingTimesCapacity)
        jsonObject.put("currentSamplingTimes", currentSamplingTimes)
        return jsonObject.toString()
    }
}